// Modules to control application life and create native browser window
const { app, BrowserWindow, Menu, Tray, dialog } = require('electron')
const path = require('node:path')
const sohoWindows = require('./service/sohoWindows')
const fileListService = require('./service/fileListService')


function createWindow () {
    const mainWindow = sohoWindows.createNotes('text.txt')
}

const newNotes = () => {
    let name = fileListService.getNewName()
    console.log("创建新notes", name)
    sohoWindows.createNotes(name)
}

const loadNotes = () => {
    let list = fileListService.listNotes()
    console.log("列表", list)
    for (let i = 0; i < list.length; i++) {
        console.log("初始化便签", list[i])
        sohoWindows.createNotes(list[i]?.name)
    }
}

let tray = null;

const createTray = () => {
    tray = new Tray(path.join(__dirname, 'res/logo.png'))
    const contextMenu = Menu.buildFromTemplate([
        { label: '新便签', click: ()=>{newNotes()} },
        { label: '显示全部', click: ()=>{ sohoWindows.showAllWindow() } },
        { label: '隐藏全部', click: ()=>{ sohoWindows.hideAllWindow() } },
        { label: '关于', click: ()=>{
                dialog.showMessageBox({
                    type: 'info',
                    title: '关于我们',
                    message: '欢迎使用Notes, 联系邮箱: i@liufang.org.cn',
                    buttons: ['确定']
                })
            } },
        // { label: '设置', type: 'radio' },
        { label: '退出', click: ()=>{ app.quit() } }
    ])
    tray.setToolTip('This is my application.')
    tray.setContextMenu(contextMenu)
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
    createTray();
    sohoWindows.init();

    // createWindow()
    loadNotes()

    app.on('activate', function () {
        // On macOS it's common to re-create a window in the app when the
        // dock icon is clicked and there are no other windows open.
        if (BrowserWindow.getAllWindows().length === 0) createWindow()
    })
})

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
    if (process.platform !== 'darwin') app.quit()
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
